/*
 * Algorithms.cpp
 *
 *  Created on: 22 Aug 2011
 *      Author: Allan
 */

#include "Algorithms.h"

// C++ includes
#include <set>

// Boost includes
#include <boost/foreach.hpp>

const unsigned IndexOf(const string& s, const vector<string>& species)
{
	return find(species.begin(), species.end(), s) - species.begin();
}

const bool IsContained(const string& s, const vector<string>& species)
{
	return IndexOf(s, species) < species.size();
}

const vector<string> Union(const vector<string>& speciesA, const vector<string>& speciesB)
{
	set<string> set;
	
	set.insert(speciesA.begin(), speciesA.end());
	set.insert(speciesB.begin(), speciesB.end());
	
	return vector<string>(set.begin(), set.end());
}

const vector<string> Intersection(const vector<string>& speciesA, const vector<string>& speciesB)
{
	vector<string> intersection;
	
	BOOST_FOREACH(auto s, speciesA)
		if(IsContained(s, speciesB))
			intersection.push_back(s);
	
	return intersection;
}

const vector<string> Difference(const vector<string>& speciesA, const vector<string>& speciesB)
{
	vector<string> difference;
	
	BOOST_FOREACH(auto s, speciesA)
		if(!IsContained(s, speciesB))
			difference.push_back(s);
	
	return difference;
}

const vector<string> Unique(const vector<string>& species)
{
	set<string> set(species.begin(), species.end());
	
	return vector<string>(set.begin(), set.end());
}
